package matImpl;

import java.io.IOException;

import uci.rt.ccr.engine.matrix.Mat;

public class PmatTest
{
	public static void main(String[] args) throws IOException 
	{

		int rows=Integer.parseInt(args[0]);
		int cols=Integer.parseInt(args[1]);
		int times=Integer.parseInt(args[2]);
		System.out.println("test Pmat:" +rows+"x"+cols + " performed:"+times+" times");
		test(rows/10,cols/10);
		multyplytest(rows,cols,times);
		
	}

	private static void multyplytest(int rows, int cols, int times) throws IOException
	{
		int treads =64;
		Mat stat = new Mat("stats",times+1,64);
		int row=rows;
		int col=cols;
		int k=0;
		double[][] data  = new double[row][col];
		for(int r=0;r<row;r++){
			for(int c=0;c<col;c++){
				data[r][c]=k++;
			}
		}
		
		
		Mat a= new Mat(data);
		Mat b = a.transpose();		
		

//		System.out.println("Start multiplyByTranspose");
//		long t0=System.currentTimeMillis();
//		for(int i=0;i<times;i++){
//			Mat c =a.multiplyByTranspose(a);
//		}	
//		long t1=System.currentTimeMillis();	
		String csvName="PmatStats_"+rows+"x"+cols+"_"+times+"_"+System.currentTimeMillis()+".csv";
		System.out.println("stored on file:"+csvName);
		for(int tr=1;tr<treads;tr=tr+1){
			stat.set(0, tr, tr);
			for(int i=0;i<times;i++){	
				long t2=System.currentTimeMillis();
				double[][] res=Pmat.pMultMT(data, data,tr);
				long t3=System.currentTimeMillis();
				
				stat.set(i+1, tr, (t3-t2));
			}
			System.out.println("Number of Threads: "+ tr+" t0="+stat.get(1, tr));
			stat.writeAsCsvFile(csvName, ",");
		}
		
		
	}

	private static void test(int rows, int cols)
	{

		int row=rows;
		int col=cols;
		int k=0;
		double[][] data  = new double[row][col];
		for(int r=0;r<row;r++){
			for(int c=0;c<col;c++){
				data[r][c]=k++;
			}
		}
		
		Mat a= new Mat(data);
		Mat b = a.transpose();		
		Mat c= a.multiply(b);
		
		
		double[][] res=Pmat.pMultMT(data, data, 2);
		
		Mat d= new Mat(res);
		System.out.println("multiplyByTranspose = Pmat --> "+c.isEqual(d));
//		c.print();
//		d.print();
//		c.substract(d).print();
		
	}
}
